package com.liuceng.zen.web.controller;

import com.liuceng.zen.pojo.domain.Form;
import com.liuceng.zen.service.FormService;
import com.liuceng.zen.service.LandingPageFormService;
import com.liuceng.zen.web.Page;
import com.liuceng.zen.web.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yanwen on 16-11-2.
 * FormController :
 */
@Controller
public class FormController extends BaseController{

    @Autowired
    public FormService formService;//表单
    @Autowired
    public LandingPageFormService landingPageFormService;

    /**
     * 表单列表
     * @return
     */
    @RequestMapping(value = "/front/forms/index",method = RequestMethod.GET)
    public String index(){
        return "/front/forms/index";
    }

    /**
     * 获取表单列表数据
     * @return
     */
    @RequestMapping(value = "/ajax/front/form/pages",method = RequestMethod.GET)
    public @ResponseBody Object  _index(Pageable pageable){
        pageable.setMemberId(getCurrentUserId());
        Page<Form> page= formService.findPage(pageable);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("rows", page.getContent());
        pageMap.put("total", page.getTotal());
        pageMap.put("offset", pageable.getOffset());//当前页
        return pageMap;
    }

    /**
     * 修改列表
     * @return
     */
    @RequestMapping(value = "/front/forms/{formId}/edit",method = RequestMethod.GET)
    public String edit(@PathVariable Integer formId, ModelMap map){
        map.put("diyFormDomain",formService.getDiyFormDomain());
        map.put("mainDomain",formService.getMainDomain());
        map.put("memberId",getCurrentUserId());
        map.put("formId",formId);
        return "/front/forms/edit";
    }

    /**
     * 修改列表
     * @return
     */
    @RequestMapping(value = "/ajax/front/forms/check_name",method = RequestMethod.GET)
    public @ResponseBody Object checkName(String name){
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("memberId",getCurrentUserId());
        Form form=formService.findOneByParams(map);
        if(form==null){
            return "success";
        }
        return "fails";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/ajax/front/forms/{formId}/delete",method = RequestMethod.POST)
    public @ResponseBody Object delete(@PathVariable Integer formId){
        //查看是否正已经有落地页使用该表单
        int count=landingPageFormService.getNumber(formId);
        if(count==0) {
            //删除
            formService.delete(formId);
            return "success";
        }else{
            return "fails";
        }

    }

    /**
     * 添加
     * @return
     */
    @RequestMapping(value = "/ajax/front/forms/add",method = RequestMethod.GET)
    public String add(ModelMap map){
        map.put("memberId",getCurrentUserId());
        return "/front/forms/_add";
    }
    /**
     * 添加
     * @return
     */
    @RequestMapping(value = "/front/forms/add",method = RequestMethod.POST)
    public @ResponseBody Object add(Form form){
        formService.insert(form);
        return form.getId();
    }
}
